
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
// </editor-fold>

public class GoogleEmail {

    private static final Logger logger = Logger.getLogger(GoogleEmail.class.getName());

    private final Session session;

// <editor-fold defaultstate="collapsed" desc="GoogleEmail()">

    public GoogleEmail() {

        // set google mailserver
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        //return new PasswordAuthentication("info@fednetworks.com", "2wsx1qazxc");
                        return new PasswordAuthentication("vasil.jo@gmail.com", "ghbdtndfcbk");
                    }
                });
        session.setDebug(false);

    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="send()">

    public void send(String from, String[] to, String subject, String body, String content) {

        try {
    	    final Message message = new MimeMessage(session);

    	    message.setFrom(new InternetAddress(from));

            InternetAddress[] bcc = new InternetAddress[to.length];
            for(int i = 0; i < to.length; i++) {
                bcc[i] = new InternetAddress(to[i]);
            }
    	    message.setRecipients(Message.RecipientType.TO, bcc);
    	    //message.setRecipients(Message.RecipientType.BCC, new InternetAddress[] { new InternetAddress("compuvest.main@gmail.com") });
            //message.setHeader("Organization", "Compuvest Corp.");
    	    message.setSubject(subject);
            message.setContent(body, content);

            new Thread() {
                @Override
                public void run() {
                    try {
                        // Send our mail message
                        Transport.send(message);
                    }
                    catch(MessagingException me) {
                        logger.log(Level.SEVERE, "Transport.send():\n{0}", me.getMessage());
                    }
                }
            }.start();
        }
        catch(MessagingException e) {
            logger.log(Level.SEVERE, "send()", e);
        }
    }
// </editor-fold>

}
